<template>
  <div>
    <el-dialog :title="title" :visible="dialogFormVisible" width="75%" @close="handleClose">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label-width="15%" label="点位名称:" prop="name">
          <el-input v-model="form.name" maxlength="15" show-word-limit placeholder="请输入" />
        </el-form-item>
        <el-form-item label-width="15%" label="所在区域:" prop="regionId">
          <el-select v-model="form.regionId" placeholder="请选择">
            <el-option v-for="(item,index) in area" :key="index" :label="item.name" :value="item.id" />

          </el-select>
        </el-form-item>
        <el-form-item label-width="15%" label="所在商圈:" prop="businessId">
          <el-select v-model="form.businessId" placeholder="请选择">
            <el-option v-for="(item,index) in businessType" :key="index" :label="item.name" :value="item.id" />

          </el-select>
        </el-form-item>
        <el-form-item label-width="15%" label="归属合作商:" prop="ownerName">
          <el-select v-model="form.ownerName" placeholder="请选择">
            <el-option v-for="(item,index) in partner" :key="index" :label="item.name" :value="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item label-width="15%" label="详细地址:" prop="addr">
          <el-cascader
            ref="inp"

            placeholder="请选择"
            size="large"
            :options="options"
            :clearable="true"
            @change="handleChange"
          />

        </el-form-item>
        <el-form-item label-width="15%">
          <el-input v-model="form.remark" type="textarea" maxlength="60" show-word-limit height="15%" placeholder="请输入详细地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button :loading="loading" type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addPointList, editStreet } from '@/api/point'
import { regionDataPlus } from 'element-china-area-data'

export default {
  props: {
    dialogFormVisible: {
      type: Boolean,
      required: true,
      default: false,
      loading: false
    },
    businessType: {
      type: Array,
      required: true
    },
    partner: {
      type: Array,
      required: true
    },
    area: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      form: {
        name: '', addr: '', areaCode: '', createUserId: '', regionId: '', businessId: '', ownerId: '', ownerName: ''
      },

      rules: { name: [{ required: true, message: '点位名称不得为空', trigger: 'blur' }],
        regionId: [{ required: true, message: '所在区域不得为空', trigger: 'blur' }],
        businessId: [{ required: true, message: '所在商圈不得为空', trigger: 'blur' }],
        ownerName: [{ required: true, message: '归属合作商不得为空', trigger: 'blur' }],
        addr: [{ required: true, message: '详细地址不得为空', trigger: 'blur' }]
      },
      loading: false,
      options: regionDataPlus,
      selectedOptions: [],
      idd: []
    }
  },
  computed: {
    title() {
      return this.form.id ? '编辑点位' : '新增点位'
    },
    submit() {
      return this.form.id ? this.editstreet : this.addstreet
    }
  }, created() {
    console.log(this.form)
    console.log(this.$store.state.user.loginDat.userId)
    this.form.createUserId = this.$store.state.user.loginDat.userId
  }, beforeUpdate() {
    this.form.createUserId = this.$store.state.user.loginDat.userId
  },
  methods: {
    handleClose() {
      this.$emit('update:dialogFormVisible', false)
      this.$refs.form.resetFields()
      this.form = {
        name: '', addr: '', areaCode: '', createUserId: '', regionId: '', businessId: '', ownerId: 1, ownerName: ''
      }
    }, handleChange(value) {
      this.form.areaCode = value[2]
      this.partner.forEach(item => {
        if (item.name === this.form.ownerName) {
          this.form.ownerId = item.id
        }
      })
    },
    async addstreet() {
      this.form.addr = this.$refs.inp.inputValue
      try {
        await this.$refs.form.validate()
        this.loading = true
        await addPointList(this.form)
        console.log(this.$parent.getList(1))
        this.$message.success('新增成功')
        this.handleClose()
      } catch (e) {
        console.log(e)
        this.$message.info('新增失败')
      } finally {
        this.loading = false
      }
    },
    async editstreet() {
      this.form.addr = this.$refs.inp.inputValue

      try {
        await this.$refs.form.validate()
        this.loading = true
        await editStreet(this.form.id, this.form)
        this.$message.success('编辑成功')
        this.$parent.getList(this.$parent.newdata.pageIndex)
        this.handleClose()
      } catch (e) {
        console.log(e)
      } finally {
        this.loading = false
      }
    }
  }

}
</script>

<style>

</style>
